import { _decorator, Component, Node, ProgressBar, UITransform, v3 } from 'cc';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import GlobalPool from '../../Init/Tools/GlobalPool';
const { ccclass, property } = _decorator;

@ccclass('LevelProgress')
export class LevelProgress extends Component {
    @property(ProgressBar)
    progressBar: ProgressBar = null;
    @property(Node)
    markLayer: Node = null;

    onEnable() {
        this.progressBar.progress = 0;
        GlobalPool.putAllChildren(this.markLayer);
        //设置mark
        const lenth = this.progressBar.node.getComponent(UITransform).contentSize.width;
        const _cfg = GlobalTmpData;
        for (let i = 0; i < _cfg.BossArr.length; i++) {
            const p = _cfg.BossArr[i];
            let m = GlobalPool.get('bossMark');
            m.parent = this.markLayer;
            m.setPosition(lenth * (p.y / _cfg.EndArea.p2d.y), 0);
        }

    }

    update(dt: number) {
        this.progressBar.progress = GlobalTmpData.Player.lpos2D.y / GlobalTmpData.EndArea.p2d.y;
        this.checkBossWarn(dt);
    }
    //
    checkBossWarn(dt) {

        for (let i = 0; i < GlobalTmpData.BossWarnRec.length; i++) {
            const rec = GlobalTmpData.BossWarnRec[i];
            if (!rec.isWarn && GlobalTmpData.Player.lpos2D.y >= rec.p.y - 900) {
                rec.isWarn = true;
                EventManager.emit(EventTypes.EffectEvents.showUIEffect, { t: GlobalEnum.Effect2DType.BossWarn, p: v3(), })
            }
        }
    }
}

